package com.zmn.plat.admin.controller.common.organize;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zmn.common.dictionary.DictModel;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.common.dto.organize.OrganizeDRO;
import com.zmn.mcc.dubbo.interfaces.organize.OrganizeListRemoteService;
import com.zmn.plat.common.dictionary.CompanyDict;
import com.zmn.plat.dubbo.utils.DubboConsts;

/**
 * @author tanbiao
 * @description
 * @since 2020/6/24 10:49
 * @modifier
 **/
@Controller
@RequestMapping("/common/organize/")
public class CommonOrganizeListController {

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private OrganizeListRemoteService organizeListRemoteService;

    /**
     * region 组织架构联动
     *
     * @param parentId
     */
    @RequestMapping(value = "listChilds")
    @ResponseBody
    public ResponseDTO<List<VtDTO>> listChilds(@RequestParam(value = "parentId", defaultValue = "0") Integer parentId) {

        List<VtDTO> list = new ArrayList<>();

        if (!NumberUtil.isPositiveInteger(parentId)) {
            return ResponseDTO.success(list);
        }

        ResponseDTO<List<OrganizeDRO>> data = organizeListRemoteService.listByParentId(parentId);
        if (data.isSuccess()) {
            List<OrganizeDRO> dataList = data.getData();
            if (!CollectionUtils.isEmpty(dataList)) {
                list.addAll(dataList.stream().map(e -> new VtDTO(e.getOrgId(), e.getOrgName())).collect(Collectors.toList()));
            }
        }

        return ResponseDTO.success(list);
    }

    /**
     * region 公司类型子类联动
     *
     * @param parentType
     */
    @RequestMapping(value = "listChildTypes")
    @ResponseBody
    public ResponseDTO<List<DictModel>> listChildTypes(@RequestParam(value = "parentType", defaultValue = "0") Integer parentType) {

        List<DictModel> list = new ArrayList<>();

        if (NumberUtil.isNullOrZero(parentType)) {
            return ResponseDTO.success(list);
        }

        return ResponseDTO.success(CompanyDict.listOrgTypeByParent(parentType));
    }
}
